package com.ysq.controller;

import com.ysq.controller.result.Result;
import com.ysq.entity.ShoppingCart;
import com.ysq.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: ShoppingCartController
 * Package: com.ysq.controller
 * Description:
 *
 * @Author YSQ
 * @Create 2023/1/12 10:09
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    public ShoppingCartService service;

    /**
     * 用户端购物车展示商品
     *
     * @return 返回统一结果封装类
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        //调用业务层方法完成购物车商品查询
        List<ShoppingCart> list = service.list();
        return Result.success(list);
    }

    /**
     * 用户端购物车添加商品
     *
     * @param shoppingCart 前端传来的要添加的商品参数
     * @return 返回统一结果封装类
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody ShoppingCart shoppingCart) {
        //调用业务层方法完成商品添加
        boolean flag = service.add(shoppingCart);
        return flag ? Result.success("添加成功~~~") : Result.error("添加失败~~~");
    }

    /**
     * 用户端购物车减少商品
     *
     * @param shoppingCart 前端传来的要减少的商品ID
     * @return 返回统一结果封装类
     */
    @PostMapping("/sub")
    public Result<String> sub(@RequestBody ShoppingCart shoppingCart) {
        //调用业务层方法完成商品减少
        boolean flag = service.sub(shoppingCart);
        return flag ? Result.success("减少成功~~~") : Result.error("减少失败~~~");
    }

    /**
     * 用户端购物车商品清空
     *
     * @return 返回统一结果封装类
     */
    @DeleteMapping("/clean")
    public Result<String> clean() {
        //调用业务层方法完成当前ID对应的购物车所有商品数据删除
        boolean flag = service.clean();
        return flag ? Result.success("购物车清空成功~~~") : Result.error("购物车清空失败~~~");
    }
}
